/*
 * This file is part of quickLoad, a software for gamers
 *
 * Copyright (C) 2005-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.IO;
using System.Windows.Forms;

namespace QuickLoad.UI
{
    public partial class FormLogViewer : Form
    {
        private ESLFileUploader.Uploader up;
        public FormLogViewer(ESLFileUploader.Uploader up)
        {
            InitializeComponent();
            this.up = up;
        }
        public FormLogViewer(string path)
        {
            InitializeComponent();
            StreamReader s = null;
            string result;
            try
            {
                s = new StreamReader(path);
                result = s.ReadToEnd();
            }
            catch(Exception ex)
            {
                result = ex.Message;
            }
            finally
            {
                if(s != null)
                    s.Close();
            }
            rtLog.Text = result;
        }

        private void timerRefresh_Tick(object sender, EventArgs e)
        {
            if (up != null && up.Status == ESLFileUploader.UploadStatus.Running)
            {
                rtLog.SelectAll();
                rtLog.SelectedText = up.getLog();
                rtLog.DeselectAll();
                //rtLog.Text = up.getLog();
                Text = lang.LogTitle + " - monitoring uploader for " + up.MatchURL;
                timerRefresh.Enabled = true;
            }
            else
            {
                Text = lang.LogTitle + " - " + lang.LogTitle2;
                timerRefresh.Enabled = false;
            }
        }

        private void nRefreshRate_ValueChanged(object sender, EventArgs e)
        {
            timerRefresh.Interval = (int)nRefreshRate.Value * 1000;
        }

        private void FormLogViewer_FormClosed(object sender, FormClosedEventArgs e)
        {
            Dispose();
        }

        private void FormLogViewer_Load(object sender, EventArgs e)
        {
            lRefreshRate1.Text = lang.RefreshInterval;
            timerRefresh_Tick(this,e);
        }
    }
}